blob: 13f664550be01d7fab3757732634dd02e56c723e [file] [log] [blame]
Ryosuke Niwaf7341962022-08-05 06:48:121<?xml version="1.0" encoding="utf-8"?>
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<title>Custom Elements: create an element for a token must increment and decrement document's throw-on-dynamic-markup-insertion counter</title>
5<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org" />
6<meta name="assert" content="Invoking document.open, document.write, document.writeln, and document.write must throw an exception when the HTML parser is creating a custom element for a token" />
7<meta name="help" content="https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token" />
8<meta name="help" content="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#throw-on-dynamic-markup-insertion-counter" />
9<script src="/resources/testharness.js"></script>
10<script src="/resources/testharnessreport.js"></script>
11<script src="resources/custom-elements-helpers.js"></script>
12</head>
13<body>
14<div id="log"></div>
15<script>
16<![CDATA[
17
18async function custom_element_reactions_in_parser(test, code)
19{
20 window.executed = false;
21 window.exception = false;
22 const content_window = await create_window_in_test_async(test, 'application/xml', `<?xml version="1.0" encoding="utf-8"?>
23<html xmlns="http://www.w3.org/1999/xhtml">
24<head>
25<script>
26<![CDATA[
27let executed = false;
28let exception = null;
29class CustomElement extends window.HTMLElement {
30 constructor() {
31 super();
32 try {
33 ${code}
34 } catch (error) {
35 exception = error;
36 }
37 executed = true;
38 }
39}
40CustomElement.observedAttributes = ['title'];
41customElements.define('some-element', CustomElement);
42]]` + `>
43</` + `script>
44</head>
45<body>
46<some-element title="some title"></some-element>
47<script>
48top.executed = executed;
49top.exception = exception;
50</script>
51</body>
52</html>`);
53 let content_document;
54 try {
55 content_document = content_window.document;
56 } catch (error) { }
57 assert_true(executed, 'Must immediately process custom element reactions for setting attributes');
58 return {window: content_window, document: content_document, exception};
59}
60
61promise_test(async function () {
62 const result = await custom_element_reactions_in_parser(this, `document.open()`);
63 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
64}, 'document.open() must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
65
66promise_test(async function () {
67 const result = await custom_element_reactions_in_parser(this, `document.open('text/html')`);
68 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
69}, 'document.open("text/html") must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
70
71// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-open-window
72promise_test(async function () {
73 let load_promise = new Promise((resolve) => window.onmessage = (event) => resolve(event.data));
74 const url = top.location.href.substring(0, top.location.href.lastIndexOf('/')) + '/resources/navigation-destination.html';
75 const result = await custom_element_reactions_in_parser(this, `document.open('${url}', '_self', '')`);
76 assert_equals(result.exception, null);
77 assert_equals(await load_promise, 'didNavigate');
78}, 'document.open(URL) must NOT throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
79
80promise_test(async function () {
81 const result = await custom_element_reactions_in_parser(this, `document.close()`);
82 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
83}, 'document.close() must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
84
85promise_test(async function () {
86 const result = await custom_element_reactions_in_parser(this, `document.write('<b>some text</b>')`);
87 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
88 assert_equals(result.document.querySelector('b'), null, 'Must not insert new content');
89 assert_false(result.document.body.innerHTML.includes('some text'), 'Must not insert new content');
90}, 'document.write must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
91
92promise_test(async function () {
93 const result = await custom_element_reactions_in_parser(this, `document.writeln('<b>some text</b>')`);
94 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
95 assert_equals(result.document.querySelector('b'), null, 'Must not insert new content');
96 assert_false(result.document.body.innerHTML.includes('some text'), 'Must not insert new content');
97}, 'document.writeln must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
98
99promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08100 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12101 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.open()`);
102 assert_equals(result.exception, null);
103}, 'document.open() of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
104
105promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08106 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12107 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.open('text/html')`);
108 assert_equals(result.exception, null);
109}, 'document.open("text/html") of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
110
111promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08112 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12113 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.close()`);
114 assert_equals(result.exception, null);
115}, 'document.close() of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
116
117promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08118 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12119 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.write('<b>some text</b>')`);
120 assert_equals(result.exception, null);
121 assert_equals(another_window.document.querySelector('b').outerHTML, '<b>some text</b>');
122}, 'document.write of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
123
124promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08125 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12126 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.writeln('<b>some text</b>')`);
127 assert_equals(result.exception, null);
128 assert_equals(another_window.document.querySelector('b').outerHTML, '<b>some text</b>');
129}, 'document.writeln of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
130
131]]>
132</script>
133</body>
134</html>